package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.dto.GoodsSalesDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.DailyOrdersCount;
import com.sky.entity.Orders;
import org.apache.ibatis.annotations.MapKey;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;


import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Mapper
public interface OrderMapper {
    /**
     * 插入订单数据
     * @param orders
     */
    void insert(Orders orders);

    /**
     * 根据订单号查询订单
     * @param orderNumber
     */
    @Select("select * from orders where number = #{orderNumber}")
    Orders getByNumber(String orderNumber);

    /**
     * 修改订单信息
     * @param orders
     */
    void update(Orders orders);

    /**
     * 历史订单分页查询
     * @param ordersPageQueryDTO
     * @return
     */
    Page<Orders> getPage(OrdersPageQueryDTO ordersPageQueryDTO);

    /**
     * 根据orderId查询订单
     */
    @Select("select * from orders where id = #{orderId}")
    Orders getById(Long orderId);

    /**
     * 统计待配送订单数量
     * @param status
     * @return
     */
    @Select("select count(id) from orders where status = #{status}")
    Integer countStatus(Integer status);

    /**
     * 根据状态以及下单时间获取订单
     * @param status
     * @param orderTime
     * @return
     */
    @Select("select * from orders where status = #{status} and order_time < #{orderTime}")
    List<Orders> getByStatusAndOrderTime(Integer status, LocalDateTime orderTime);

    /**
     * 每天查一次
     * @param begin
     * @param end
     * @param status
     * @return
     */
    Double getAmountByDate(LocalDateTime begin, LocalDateTime end, Integer status);


    List<Double> getAmountByDate2(LocalDate begin, LocalDate end, Integer status);

    /**
     * 订单统计
     * @param startDateTime
     * @param endDateTime
     * @return
     */
    List<DailyOrdersCount> getAmount(LocalDateTime startDateTime, LocalDateTime endDateTime);

    /**
     * 查询订单总数
     * @param startDateTime
     * @param endDateTime
     * @return
     */
    @Select("select count(*) from orders where order_time >= #{startDateTime} and order_time < #{endDateTime} and status >= #{status}")
    Integer getTotalOrders(LocalDateTime startDateTime, LocalDateTime endDateTime, Integer status);

    /**
     * 查询有效订单总数
     * @param startDateTime
     * @param endDateTime
     * @param status
     * @return
     */
    @Select("select count(*) from orders where order_time >= #{startDateTime} and order_time < #{endDateTime} and status = #{status}")
    Integer getTotalValidOrders(LocalDateTime startDateTime, LocalDateTime endDateTime, Integer status);

    /**
     * 每日订单总数
     * @param begin
     * @param end
     * @param status
     * @return
     */
    @Select("select DATE(order_time) as date , COUNT(*) as count from orders where order_time >= #{begin} and order_time < #{end} and status >= #{status} group by DATE(order_time)")
    List<DailyOrdersCount> getDailyOrders(LocalDateTime begin, LocalDateTime end, Integer status);

    /**
     * 每日有效订单总数
     * @param begin
     * @param end
     * @param status
     * @return
     */
    @Select("select DATE(order_time) as date , COUNT(*) as count from orders where order_time >= #{begin} and order_time < #{end} and status = #{status} group by DATE(order_time)")
    List<DailyOrdersCount> getDailyValidOrders(LocalDateTime begin, LocalDateTime end, Integer status);

    /**
     * 查询销量前十
     * @param begin
     * @param end
     * @return
     */
    List<GoodsSalesDTO> getTop10(LocalDateTime begin, LocalDateTime end);

    /**
     * 根据动态条件统计订单数量
     * @param map
     * @return
     */
    Integer countByMap(Map map);

}
